/*
 *  Copyright 2009 Danny Lagrouw
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package org.bastion.domain.service.impl

import java.io.File
import java.io.FileReader
import java.io.Reader
import java.util.Properties
import org.bastion.domain.service.ConfigService
import scala.collection.jcl.Hashtable
import scala.util.matching.Regex

class PropertiesFileConfigService(val properties: Properties) extends ConfigService {

  val propMap = new Hashtable(properties)
  
  def this(propertiesReader: Reader) = {
    this(new Properties)
    properties.load(propertiesReader)
  }
  
  def this(propertiesFile: File) = {
    this(new FileReader(propertiesFile))
  }

  override def getConfig(propertyFilter: String): Option[String] = {
    val p = properties.getProperty(propertyFilter)
    if (p == null) None else Some(p)
  }

  override def getConfig(propertyFilter: Regex): scala.collection.Map[String, String] = {
    var map = scala.collection.mutable.Map.empty[String,String]
    propMap filter {
      case (key: String, value) => propertyFilter.findFirstIn(key) != None
    } foreach {
      case (k:String,v:String) => map(k) = v
    }
    map.readOnly
  }

}
